using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using zhangmenren.IService;
using zhangmenren.Models;
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
using zhangmenren.Models.Enums;
using zhangmenren.Models.help;
using zhangmenren.Untils;

namespace zhangmenren.Controllers
{
	public class QiYeController : Controller
	{
		private IBannerService _bannerService;

		private ICacheService _cacheService;

		private IEnterpriseService _enterpriseService;

		private IQiYeService qiYeService;

		public QiYeController(IBannerService bannerService, ICacheService cacheService, IEnterpriseService enterpriseService, IQiYeService _qiYeService)
		{
			_bannerService = bannerService;
			_cacheService = cacheService;
			_enterpriseService = enterpriseService;
			qiYeService = _qiYeService;
		}


		/// <summary>
		/// 名企名录
		/// </summary>
		/// <returns>The index.</returns>
		public async Task<IActionResult> Index()
		{

			ViewBag.Banner = await GetData.GetBanner(_bannerService, _cacheService, "QiYeBanner", BannerType.QIYE);
			var list = await GetQiye("");
			return View(list?.ToList().Take(10).ToList());
		}


		/// <summary>
		/// 企业详情
		/// </summary>
		/// <returns>The detail.</returns>
		/// <param name="id">Identifier.</param>
		public async Task<IActionResult> Detail(int id)
		{
			var qiye = await _enterpriseService.Select(id);
			return View(qiye?.FirstOrDefault());
		}

		public async Task<IActionResult> Join()
		{
			ViewBag.Banner = await GetData.GetBanner(_bannerService, _cacheService, "QiYeBanner", BannerType.QIYE);
			return View();
		}

		/// <summary>
		/// 获取企业名录
		/// </summary>
		/// <returns>The qiye.</returns>
		/// <param name="key">Key.</param>
		public async Task<IEnumerable<EnterpriseEntity>> GetQiye(string key)
		{
			IEnumerable<EnterpriseEntity> list;
			if (!string.IsNullOrEmpty(key)&& _cacheService.Exists(key))
			{
				var chche = _cacheService.Get(key);
				if (chche != null)
				{
					return (IEnumerable<EnterpriseEntity>)chche;
				}
			}
			list = await _enterpriseService.Select(true);
			if (list.Any())
			{
				_cacheService.Add(key, list);
			}
			return list;
		}

		/// <summary>
		/// 搜索数据
		/// </summary>
		/// <param name="key"></param>
		/// <returns></returns>
		[HttpPost]
		public async Task<JsonResult> Search(string key)
		{
			var list = await GetQiye("QiYeList");
			var result = list.ToList().FindAll(p => p.Name.Contains(key));
			return Json(new { state = "ok", msg = "获取成功", data = result });
		}


		[HttpPost]
		public JsonResult Add(EnterpriseEntity entity)
		{
			entity.State = 0;
			if (entity != null)
			{
				var result = _enterpriseService.Add(entity);
				return Json(new { State = "OK", Msg = "添加成功" });
			}
			return Json(new { State = "FAIL", Msg = "添加错误，请稍后重试。" });
		}


		[HttpPost]
		public JsonResult ShouChang(int id)
		{
			var user = QTCookie.GetCookie<UserCook>(HttpContext);
			if (user == null)
			{
				return Json(new { state = "login", msg = "请重新登录" });
			}
			QiYeShouChangEntity entity = new QiYeShouChangEntity();
			entity.UserId = user.Id;
			entity.QiYeId = id;
			var result = qiYeService.AddAndDel(entity);
			return Json(new { State = "OK", Msg = "操作成功" });
		}


		[HttpPost]
		public JsonResult LiuLan(int id)
		{
			QiYeShouChangEntity entity = new QiYeShouChangEntity();
			var result = _enterpriseService.AddLiuLan(id);
			return Json(new { State = "OK", Msg = "操作成功" });
		}

	}
}
